package com.itheima.health.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.itheima.health.dao.MemberDao;
import com.itheima.health.pojo.Member;
import com.itheima.health.service.MemberService;
import com.itheima.health.utils.MD5Utils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service(interfaceClass = MemberService.class)
@Transactional
public class MemberServiceImpl implements MemberService {

    @Autowired
    MemberDao memberDao;

    @Override
    public Member findMemberByTelephone(String telephone) {
        return memberDao.findMemberByTelephone(telephone);
    }

    @Override
    public void add(Member member) {
        // 如果密码不为空，使用md5进行加密
        if (member.getPassword() != null){
            member.setPassword( MD5Utils.md5(member.getPassword()));
        }
        memberDao.add(member);
    }

    @Override
    public List<Integer> findMemberCountByMonth(List<String> list) {
        // 组织数据
        List<Integer> countList = new ArrayList<>();
        // 遍历months
        for (String sMonth : list) {
            // 如果传入的格式：[2019-01, 2019-02, 2019-03, 2019-04...
            //String sDate = sMonth+"-31"; // 使用当前月，计算当前月的最后1天
            Integer count = memberDao.findMemberCountByMonth(sMonth);
            countList.add(count);
        }
        return countList;
    }
}
